﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Design;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using XCI.Windows;
using XCI.Helper;

namespace XCI.Setting.Controls
{
    public partial class SettingFormatCondition : BaseUserControl
    {
        public SettingFormatCondition()
        {
            InitializeComponent();
        }

        public Func<FormatConditionCollectionBase> GetConditions { get; set; }
        public Func<StyleFormatConditionBase> AddCondition { get; set; }
        public Func<IEnumerable<ColumnInfo>> GetColumnInfos { get; set; }

        public void Init()
        {
            gridControl1.DataSource = GetConditions?.Invoke();
            LoadFiledName();
        }

        protected void LoadFiledName()
        {
            comBoxColumn.Items.BeginUpdate();
            comBoxColumn.Items.Clear();
            comBoxColumn.Items.Add(new ImageComboBoxItem(null, null, -1));
            var columnInfos = GetColumnInfos?.Invoke();
            if (columnInfos != null)
            {
                foreach (var item in columnInfos)
                {
                    comBoxColumn.Items.Add(new ImageComboBoxItem(item.Name, item.Column, -1));
                }
            }
            comBoxColumn.Items.EndUpdate();
        }

        protected void ShowEditor(StyleFormatConditionBase condition)
        {
            using (ExpressionEditorForm form = new ConditionExpressionEditorForm(condition, null))
            {
                form.StartPosition = FormStartPosition.CenterParent;
                if (form.ShowDialog(this) == DialogResult.OK)
                {
                    condition.Expression = form.Expression.Replace(" ", "");
                }
            }
        }

        private void gridViewExpression_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            btnEdit.Enabled = btnDelete.Enabled = gridView1.FocusedRowHandle > -1;
            if (gridView1.GetRow(gridView1.FocusedRowHandle) is StyleFormatConditionBase condition)
            {
                propertyGridPlus.SelectedObject = condition.Appearance;
                propertyGridPlus.Enabled = true;
                btnEdit.Enabled = btnDelete.Enabled = true;
            }
            else
            {
                propertyGridPlus.SelectedObject = null;
                propertyGridPlus.Enabled = false;
                btnEdit.Enabled = btnDelete.Enabled = false;
            }
        }

        private void gridViewExpression_DoubleClick(object sender, EventArgs e)
        {
            btnEdit.PerformClick();
        }

        //private void gridView1_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        //{
        //    if (gridView1.OptionsView.ShowIndicator && e.Info.IsRowIndicator && e.RowHandle >= 0)
        //    {
        //        e.Info.DisplayText = (e.RowHandle + 1).ToString();
        //        e.Info.ImageIndex = -1;
        //    }
        //}

        private void btnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var condition = AddCondition();
            ShowEditor(condition);
            gridControl1.RefreshDataSource();
            gridView1.MoveLastVisible();
        }

        private void btnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gridView1.GetRow(gridView1.FocusedRowHandle) is StyleFormatConditionBase condition)
            {
                ShowEditor(condition);
            }
        }

        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要删除选中的数据吗？") == DialogResult.Yes)
            {
                gridView1.DeleteSelectedRows();
            }
        }
    }

    public class ColumnInfo
    {
        public string Name { get; set; }
        public object Column { get; set; }
    }
}
